//
//  HHBaseTabBarViewController.m
//  YSJ
//
//  Created by 花花 on 2016/12/15.
//  Copyright © 2016年 花花. All rights reserved.
//

#import "HHBaseTabBarViewController.h"
#import "HHBaseNavViewController.h"
@implementation HHBaseTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createChildControlViews];
}


-(void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];

    [self addTabBarSelectedImageAnimation];
}

- (void)addTabBarSelectedImageAnimation{

    for (UIControl *ctrl in self.tabBar.subviews) {
        if ([ctrl isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            [ctrl addTarget:self action:@selector(tabBarButtonClick:) forControlEvents:UIControlEventTouchUpInside];
            
        }
    }

}
-(void)tabBarButtonClick:(UIControl *)ctrl{
    UIView *imageView,*label;
    [self addView:imageView ctrl:ctrl classString:@"UITabBarSwappableImageView"];
    [self addView:label ctrl:ctrl classString:@"UITabBarButtonLabel"];

}

-(void)addView:(UIView *)view ctrl:(UIControl *)ctrl classString:(NSString *)classString{

    for (UIView *view in ctrl.subviews) {
        if ([view isKindOfClass:NSClassFromString(classString)]) {
            
            CAKeyframeAnimation *animation =[CAKeyframeAnimation animation];
            animation.keyPath =@"transform.scale";
            animation.values = @[@1.0,@1.3,@0.9,@1.15,@0.95,@1.02,@1.0];
            animation.duration = 1;
            animation.calculationMode = kCAAnimationCubic;
            [view.layer addAnimation:animation forKey:nil];
        }
    }
    
}
-(void)createChildControlViews{

    //控制器名字
    NSArray *controllers = @[@"HHHomeViewController",@"HHFeaturesViewController",@"HHStoreViewController",
                             @"HHShopViewController",@"HHMeViewController"];
    //TabBar图片
    NSArray *normalImgNames = @[@"YS_index_nor",@"YS_pro_nor",@"YS_shop_nor",@"YS_car_nor",@"YS_mine_nor"];
    
    //TabBar选中图片
    NSArray *selectedImgNames = @[@"YS_index_sel",@"YS_pro_sel",@"YS_shop_sel",@"YS_car_sel",@"YS_mine_sel"];

    //TabBarItem标题
    NSArray *titles = @[@"首页",@"专题",@"店铺",@"购物篮",@"我"];
    
    for (int i = 0 ; i<controllers.count; i++) {

        //1.创建对象
        UIViewController *Vc  = [[NSClassFromString(controllers[i]) alloc]init];
        
        Vc.tabBarItem.title = titles[i];
        Vc.tabBarItem.image = [[UIImage imageNamed:normalImgNames[i]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        Vc.tabBarItem.selectedImage =  [[UIImage imageNamed:selectedImgNames[i]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
        //2.设置文字显示的样式
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        dict[NSForegroundColorAttributeName]= [UIColor colorWithHexString:@"A8A8A8"];
        [Vc.tabBarItem  setTitleTextAttributes:dict forState:UIControlStateNormal];
        
        NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
        dictionary[NSForegroundColorAttributeName] = [UIColor colorWithHexString:ThemeColor ];
        [Vc.tabBarItem setTitleTextAttributes:dictionary forState:UIControlStateDisabled];
        
        HHBaseNavViewController *nav = [[HHBaseNavViewController alloc]initWithRootViewController:Vc];
//        nav.navigationBar.translucent = NO;
        [self addChildViewController:nav];
    }
}

@end
